package com.orangelife.mobile.guide.activity;

import java.util.ArrayList;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.guide.fragment.*;
import com.orangelife.mobile.guide.fragment.GuideFragment4.FinishListener;
import com.thirdsdk.zhugeio.ZhugeIOHelper;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class GuideFragmentActivity extends FragmentActivity implements
        OnClickListener {

    private ViewPager vPage;
    private GuideFragment1 fragment1;
    private GuideFragment2 fragment2;
    private GuideFragment3 fragment3;
    private GuideFragment4 fragment4;
    private ArrayList<Fragment> fragmentList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        initFragment();
        initViewPager();
        zhugeTrack();
    }

    private void zhugeTrack() {
        ZhugeIOHelper.trackEnterGuidePage(this);
    }

    private void initFragment() {
        DisplayMetrics mDisplayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
        int widthPixels = mDisplayMetrics.widthPixels;
        fragment1 = new GuideFragment1(widthPixels);
        fragment2 = new GuideFragment2(widthPixels);
        fragment3 = new GuideFragment3(widthPixels);
        fragment4 = new GuideFragment4(widthPixels);
        fragment4.setFinishListener(new FinishListener() {

            @Override
            public void onFinished() {
                GuideFragmentActivity.this.finish();
            }
        });

        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);
    }

    @SuppressLint("NewApi")
    private void initViewPager() {
        vPage = (ViewPager) findViewById(R.id.viewpager);
        FragmentManager fragmentManager = getSupportFragmentManager();
        vPage.setAdapter(new viewPageAdapter(fragmentManager, fragmentList));
        vPage.setOnPageChangeListener(new MyOnPageChangeListener());
        initDots();
    }

    public class viewPageAdapter extends FragmentStatePagerAdapter {

        ArrayList<Fragment> fragmentlist;

        public viewPageAdapter(FragmentManager fm,
                               ArrayList<Fragment> fragmentlist) {
            super(fm);
            this.fragmentlist = fragmentlist;
        }

        @Override
        public Fragment getItem(int arg0) {
            return fragmentlist.get(arg0);
        }

        @Override
        public int getCount() {
            return fragmentlist.size();
        }

        @Override
        public Object instantiateItem(ViewGroup arg0, int arg1) {
            return super.instantiateItem(arg0, arg1);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }

    }

    public class MyOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            setCurDot(arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    // 底部小店图片
    private ImageView[] dots;

    // 记录当前选中位置
    private int currentIndex;
    private LinearLayout llImage;

    private void initDots() {
        llImage = (LinearLayout) findViewById(R.id.ll);

        dots = new ImageView[fragmentList.size()];

        // 循环取得小点图片
        for (int i = 0; i < fragmentList.size(); i++) {
            // 得到一个LinearLayout下面的每一个子元素
            dots[i] = (ImageView) llImage.getChildAt(i);
            dots[i].setEnabled(true);// 都设为灰色
            dots[i].setOnClickListener(this);
            dots[i].setTag(i);// 设置位置tag，方便取出与当前位置对应
        }

        currentIndex = 0;
        dots[currentIndex].setEnabled(false);// 设置为白色，即选中状态
    }

    private void setCurView(int position) {
        if (position < 0 || position >= fragmentList.size()) {
            return;
        }

        vPage.setCurrentItem(position);
    }

    private void setCurDot(int positon) {
        if (positon < 0 || positon > fragmentList.size() - 1 || currentIndex == positon) {

            return;
        }
        if (positon == 3) {
            llImage.setVisibility(View.INVISIBLE);
        } else {
            llImage.setVisibility(View.VISIBLE);
        }
        dots[positon].setEnabled(false);
        dots[currentIndex].setEnabled(true);

        currentIndex = positon;
    }

    @Override
    public void onClick(View v) {
        int position = (Integer) v.getTag();
        setCurView(position);
        setCurDot(position);
    }
}